﻿function runTests(promise, defer) {
    describe('CrmRestKit ' + promise, function () {
        it('ASync', function (notify) {
            var xhr = new XhrStub();

            var restKit = new RestKit.RestKit(defer, function () {
                return xhr;
            }, XrmContextStub);

            var result = '';
            restKit.Retrieve('test', '', [], true).then(function (v) {
                result = 'SUCCES: ' + JSON.stringify(v);
                result.should.equal('SUCCES: {"d":{"name":"test"}}');
                notify();
            }, null).then(null, function (v) {
                result = 'FAIL: ' + JSON.stringify(v);

                notify();
            });

            setTimeout(function () {
                return xhr.readyStateChange(200, 4, '{"d":{"name":"test"}}');
            }, 20);
        });

        it('Sync', function () {
            var xhr = new XhrStub();

            var restKit = new RestKit.RestKit(defer, function () {
                return xhr;
            }, XrmContextStub);

            var result = '';
            restKit.Retrieve('test', '', [], true).then(function (v) {
                result = 'SUCCES: ' + JSON.stringify(v);
            }, function (v) {
                result = 'FAIL: ' + JSON.stringify(v);
            });

            xhr.readyStateChange(200, 4, '{"d":{"name":"test"}}');

            result.should.equal('SUCCES: {"d":{"name":"test"}}');
        });
    });
}

runTests('BlueBird', function () {
    return new RestKit.BlueBirdAdaptor(Promise.defer());
});
runTests('Q', function () {
    return Q.defer();
});
runTests('JQuery', function () {
    return new RestKit.JQueryDeferAdaptor(($).Deferred());
});
runTests('VerySimple', function () {
    return new RestKit.VerySimplePromise();
});

var XhrStub = (function () {
    function XhrStub() {
    }
    XhrStub.prototype.readyStateChange = function (status, readyState, body) {
        this.responseBody = body;
        this.responseText = body;
        this.status = status;
        this.readyState = readyState;

        this.onreadystatechange(null);
    };

    XhrStub.prototype.open = function (method, url, async, user, password) {
    };
    XhrStub.prototype.setRequestHeader = function (header, value) {
    };
    XhrStub.prototype.send = function (data) {
    };
    return XhrStub;
})();
function XrmContextStub() {
    return {
        getServerUrl: function () {
            return '';
        },
        isOutlookClient: function () {
            return false;
        },
        isOutlookOnline: function () {
            return false;
        }
    };
}
//# sourceMappingURL=C:/Users/live_000/Source/Workspaces/CrmRestKit/Output/scripts/3.x/TypeScript/CrmRestKit.Tests.js.map
